package com.lxw.test1.mutiDownload;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.*;

public class TestPool2 {
    //核心线程数
    static int corePoolSize = 3;
    //最大线程数
    static int maximumPoolSize = 6;
    //超过 corePoolSize 线程数量的线程最大空闲时间
    static long keepAliveTime = 2;
    //以秒为时间单位
    static TimeUnit unit = TimeUnit.SECONDS;
    //对应创建工作队列，用于存放提交的等待执行任务，此处填写队列大小
    static int queueSize = 20000;
    public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
        // 定义开始时间
        long start = System.currentTimeMillis();




        // 1、创建服务，创建线程池
        //
        int coreSize = 5;
        ExecutorService service = Executors.newFixedThreadPool(coreSize);
//        ThreadPoolExecutor executor = new ThreadPoolExecutor(args);





        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                unit,
                new ArrayBlockingQueue<>(queueSize),
                new ThreadPoolExecutor.AbortPolicy()
        );







        // 读取txt文件
        String urlText = FileUtils.readFileToString(new File("D:/CodeReference/img/1234.txt"), "UTF-8");
        String[] urls = urlText.split("\n");

        for (int i = 0; i < urls.length-5; i = i + 1) {

            MyCallable t1 = new MyCallable(urls[i]);
//            MyCallable t2 = new MyCallable(urls[i+1]);
//            MyCallable t3 = new MyCallable(urls[i+2]);
//            MyCallable t4 = new MyCallable(urls[i+3]);
//            MyCallable t5 = new MyCallable(urls[i+4]);


            Future s1 = executor.submit(t1);
            Object ss1 = s1.get();



//            Future<Boolean> s1 = service.submit(t1);
//            Future<Boolean> s2 = service.submit(t2);
//            Future<Boolean> s3 = service.submit(t3);
//            Future<Boolean> s4 = service.submit(t4);
//            Future<Boolean> s5 = service.submit(t5);
//            boolean ss1 = s1.get();
//            boolean ss2 = s2.get();
//            boolean ss3 = s3.get();
//            boolean ss4 = s4.get();
//            boolean ss5 = s5.get();


            System.out.println("executor"+ss1);
//            System.out.println(ss2);
//            System.out.println(ss3);
//            System.out.println(ss4);
//            System.out.println(ss5);

        }


//         2、关闭连接
        executor.shutdown();
//        service.shutdown();
        long end = System.currentTimeMillis();
        System.out.println("线程下载数据用时:" + (end - start) + "ms");
    }


    public static List<List<String>> cut(String[] array, int count) {
//            int count = array.length % num == 0 ? array.length / num : array.length / num + 1;
        int num = array.length % count == 0 ? array.length / count : array.length / count + 1;
        List<List<String>> arrayList = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            int index = i * num;
            List<String> list = new ArrayList<>();
            int j = 0;
            while (j < num && index < array.length) {
                list.add(array[index++]);
                j++;
            }
            arrayList.add(list);
        }
        return arrayList;
    }


}






